/*

This code merges ACS/FRED 1980 characteristics for Bartik IV tests

Last edited: 3/8/2020 by Katherine Wagner

*/

clear all

gl dataRAW  "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\dataRAW\"
gl dataSTATA  "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\dataSTATA\"
gl results "C:\Users\User\Dropbox\work_in_progress\carbon_lockin\results\"

import delimited "${dataSTATA}fred\merged_ACS_FRED.csv", clear
merge 1:1 year state using "${dataSTATA}seds\seds_iv.dta", assert(1 2 3) keep(3) nogen

replace percappersincome = percappersincome / 1000
replace population = population / 1000
replace mean_hh_income = mean_hh_income / 1000
la var unemploymentfred "unemployment rate, from FRED"
la var percappersincome "per capital personal income, 1000 1980 dollars, from FRED"
la var population "population, millions, from FRED"
la var mean_hh_income "mean household income, 1000 1980 dollars, from ACS"
 

eststo: reg L0elecShareB_cl unemploymentfred percappersincome mean_hh_income any_college_attended perc_white perc_black population occupants_per_hh
eststo: reg L0elecShareB_ng unemploymentfred percappersincome mean_hh_income any_college_attended perc_white perc_black population occupants_per_hh
eststo: reg L0elecShareB_pa unemploymentfred percappersincome mean_hh_income any_college_attended perc_white perc_black population occupants_per_hh

esttab using "${results}tables\at1_bartiktests.tex", b(3) se(3) obslast replace star(* 0.10 ** 0.05 *** 0.01) //
	keep(unemploymentfred percappersincome mean_hh_income any_college_attended perc_white perc_black population occupants_per_hh) mtitles("Coal Share" "Natural Gas Share" "Petroleum Share") //
	coeflabels(unemploymentfred Unemployment_Rate percappersincome State_Per_Capita_Income_1000s mean_hh_income Mean_Household_Income_1000s any_college_attended Share_Any_College_Education perc_white Share_White perc_black Share_Black population Population_1000s occupants_per_hh Household_Size) scalars(r2) sfmt(3)
eststo clear


* 2010 data -- discussed but not in paper

* loop over and read in csvs

cd "${dataRAW}fred\2010\"
cap erase "${dataRAW}fred\2010\fred2010.dta"

* loop over csv files only 
local files : dir . files "*.csv"
local i =  0

foreach f of local files {

	drop _all
	insheet using "`f'"
	display "`f'"
	gen year = substr(date,1,4)
	destring year, replace
	keep if year == 2010
	foreach var of varlist *pop {
		local newname = substr("`var'",1,2)
		gen state = "`newname'"
	}
	ren *pop pop
	ren *pcpi pcpi
	ren la unemp
	if `i' > 0 append using "${dataRAW}fred\2010\fred2010.dta"
	save "${dataRAW}fred\2010\fred2010.dta", replace
	local i = 1
	sleep 200
}
duplicates drop state, force
replace state = strupper(state)

merge 1:1 year state using "${dataSTATA}seds\seds_iv.dta", assert(1 2 3) keep(3) nogen

replace pcpi = pcpi / 1000
replace pop = pop / 1000

eststo: reg L0elecShareB_cl pcpi unemp pop
eststo: reg L0elecShareB_ng pcpi unemp pop
eststo: reg L0elecShareB_pa pcpi unemp pop














import delimited "${dataRAW}acs\2010\usa_00005.csv\data.csv", clear
collapse (mean) [aw=hhwt], by(statefp)
merge 1:1 year state using "${dataSTATA}seds\seds_iv.dta", assert(1 2 3) keep(3) nogen

